﻿package net.wangit;

import java.util.List;

import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.framework.session.AgileSession;
import net.wangit.framework.session.UnitSettings;
import net.wangit.framework.util.ActionCommandUtil;
import net.wangit.framework.util.QueryProcessor;
import net.wangit.framework.util.UnitDataUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBImplementor;
import net.wangit.jdbc.JDBCException;
import net.wangit.jdbc.QueryParameters;
import net.wangit.jdbc.Table;
import net.wangit.jdbc.executable.VersionChecker;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitFactory;
import net.wangit.view.unit.util.UnitUtil;


/**
 * 提供常用接口。
 * 
 * @author watson Feb 24, 2010
 */
public class Wangit {

	private Wangit() {
	}

	// 执行自定义的Action命令行。
	public static final void doActionCommand(DB db, ActionContext ac,
			String domain, String command) throws JDBCException {
		List<Table> tables = ActionCommandUtil.load(ac, domain, command);
		if (!Checker.isEmpty(tables))
			db.save(tables);
	}

	// 解析sql语句中的动态表达式
	public static final String resolveSQL(ActionContext ac, String sql) {
		return UnitDataUtil.resolveSQL(ac, sql);
	}

	// 获得列表单元的查询参数对象，设置了分页参数
	public static QueryParameters getQueryParameters(ActionContext ac, DB db,
			String sql, String domain, String listUnitId) throws JDBCException {
		return UnitDataUtil.getQueryParameters(ac, db, sql, domain, listUnitId);
	}

	// 获取列表单元的页号，不存在时返回0
	public static int getListPageNumber(ActionContext ac, String domain,
			String listUnitId) {
		AgileSession session = ActionContextHelper.getSession(ac);
		UnitSettings us = session.getUnitSettings(domain, listUnitId);
		if (us != null)
			return us.getPageNumber();
		else
			return 0;
	}

	// 获取列表单元的总记录数，不存在时返回0
	public static int getListMaxSize(ActionContext ac, String domain,
			String listUnitId) {
		AgileSession session = ActionContextHelper.getSession(ac);
		UnitSettings us = session.getUnitSettings(domain, listUnitId);
		if (us != null)
			return us.getMaxSize();
		else
			return 0;
	}

	// 为指定的查询语句附加查询条件和排序字段
	public static String addConditionAndOrder(ActionContext ac, DB db,
			String sql, String domain, String queryUnitId, String tableName,
			String tableAlias) {
		return QueryProcessor.addConditionAndOrder(ac, (DBImplementor) db, sql,
				domain, queryUnitId, tableName, tableAlias);
	}

	// 获得查询单元的数据。
	public static Row getQueryRow(ActionContext ac, String domain, String unitId) {
		AgileSession session = ActionContextHelper.getSession(ac);
		UnitSettings us = session.getUnitSettings(UnitUtil.getKey(domain,
				unitId));
		MapList data = us.getData();
		return data != null ? data.getRow(0) : null;
	}

	// 清除指定单元的缓存数据（查询单元和启用了“记忆”的单元的数据会被系统缓存）。
	public static void clearUnitCache(ActionContext ac, String domain,
			String unitId) {
		AgileSession session = ActionContextHelper.getSession(ac);
		UnitSettings us = session.getUnitSettings(UnitUtil.getKey(domain,
				unitId));
		us.setData(null);
		Unit unit = UnitFactory.getUnitWithoutClone(domain, unitId);
		if (unit != null) {
			for (int i = 0; i < unit.size(); i++) {
				String elementId = unit.getElement(i).getId();
				ac.setRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".").append(elementId)
						.toString(), "");
			}

		}
	}

	// 更新、删除前验证是否脏数据。
	public static boolean isDirtyData(DB db, ActionContext ac, String domain,
			String tableName, String unitId) throws JDBCException {
		return VersionChecker.isDirty((DBImplementor) db, ac, domain,
				tableName, unitId);
	}
}
